//index.js
//获取应用实例
const app = getApp()
const Util = require('../../utils/util.js');
Page({
  data: {
    images: [
      { url: false },
      { url: true },
      { url: true }
    ],
    oneData: [],
    userData:[],
    token:'',
    cateId:'',
    userNumber:1
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (e) {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data,
          cateId: e.cate_id
        });
        that.getCateRoleUser();
      }
    });
  },
  getCateRoleUser: function () {
    var that = this;
    var cat_id = that.data.cateId;
    var token = that.data.token;
    Util.getCateRoleUser({
      "cate_id": cat_id,
      "token": token
    }, function (re) {
      console.info(re);
      if (re.code == 200) {
        that.setData({
          userData: re.data,
          oneData:re.user,
          userNumber: re.data.length+1
        });
      }
      else {
        Util.showMsgDialog(re.msg);
      }
    }, function (err) {
      Util.showMsgDialog("操作失败");
    });
  },
  checkUserData:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var userData = that.data.userData;
    if (userData[index]['check']){
      userData[index]['check'] = false;
    }else{
      userData[index]['check'] = true;
    }
    that.setData({
      userData: userData
    });
  },
  subUserData:function(){
    var that = this;
    var userId = '';
    var userData = that.data.userData;
    for (var i = 0; i < userData.length;i++){
      if (userData[i]['check']){
        userId = userId + userData[i].id + ',';
      }
    }
    var that = this;
    var cat_id = that.data.cateId;
    var token = that.data.token;
    Util.setCateRoleUser({
      "cate_id": cat_id,
      "token": token,
      "user_id": userId
    }, function (re) {
      console.info(re);
      if (re.code == 200) {
        wx.showToast({
          title: re.msg,
          icon: 'success',
          duration: 2000
        })
      }
      else {
        Util.showMsgDialog(re.msg);
      }
    }, function (err) {
      Util.showMsgDialog("操作失败");
    });  
  },
  moreFunc:function(){
    wx.navigateTo({
      url: 'test?id=1'
    })
  },
  addPhoto:function(){
    this.setData({
      addPhoto: false,
      focus: true
    })
  },
  hidePhoto: function () {
    this.setData({
      addPhoto: true,
      focus: false
    })
  },
  settingImages:function(){
    wx.navigateTo({
      url: 'manage'
    })
  },
  scrolltoupper :function(){
    wx.startPullDownRefresh();
  },
  upper: function (e) {
    console.log(e)
  },
  lower: function (e) {
    console.log(e)
  },
  scroll: function (e) {
    console.log(e)
  },
  onPullDownRefresh:function(){
    wx.startPullDownRefresh();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 300);
  }
})
